package patrol.mobile.com.mobilepatrol.activity.monitor;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.Item;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.activity.toll.add_data.AddDataPage2Activity;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.OrgPostTemplateListBean;
import patrol.mobile.com.mobilepatrol.bean.ShebeiXunJianBean;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;

/**
 * Created by z on 2018/9/18.
 * Des: 机电巡检明细页面
 */

public class JdxjDetailActivity extends BaseActivity {
    @BindView(R.id.detailName)
    TextView detailName;
    @BindView(R.id.detailTime)
    TextView detailTime;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    private String id;

    private ShebeiXunJianBean shebeiXunJianBean;
    private MyAdapter myAdapter;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_jdxj_detail_layout;
    }

    @Override
    public void initTitle() {
        super.initTitle();
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText(R.string.mingxiyemian);
    }

    @Override
    public void initData() {
        super.initData();
        id = getIntent().getStringExtra("id");
        if (!TextUtils.isEmpty(id)) {
            getHttpData();
        }
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(myAdapter = new MyAdapter());

    }

    private void getHttpData() {
        HashMap<String, String> hashMap = new HashMap();
        hashMap.put("id", id);
        HttpSender sender = new HttpSender(this, baseUrl, Urls.getJiankkElefailinsprecEntity);
        sender.requestPost(hashMap, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                shebeiXunJianBean = new Gson().fromJson(response, ShebeiXunJianBean.class);
                manageData(shebeiXunJianBean);
                myAdapter.setShebeiXunJianBean(shebeiXunJianBean);
                detailName.setText("巡检人:" + shebeiXunJianBean.getAccount().getName());
                detailTime.setText("日期:" + shebeiXunJianBean.getDate());
                detailTime.setVisibility(View.GONE);
            }
        });
    }
    private void manageData(ShebeiXunJianBean bean) {
        List<ShebeiXunJianBean.ParentBean> parentBeanList = new ArrayList<>();
        int i = 0;
        int j = 0;
        ShebeiXunJianBean.ParentBean parentBean;
        List<ShebeiXunJianBean.JiankkElefailinsprecDivListBean> listBeanList;
        for (; i < bean.getJiankkElefailinsprecDivList().size(); i++) {
            parentBean = new ShebeiXunJianBean.ParentBean();
            listBeanList = new ArrayList<>();
            parentBean.setProLarge(bean.getJiankkElefailinsprecDivList().get(i).getPosition().trim());
            listBeanList.add(bean.getJiankkElefailinsprecDivList().get(i));
            j = i + 1;
            for (; j < bean.getJiankkElefailinsprecDivList().size(); j++) {
                if (bean.getJiankkElefailinsprecDivList().get(i).getPosition().trim().equals(bean.getJiankkElefailinsprecDivList().get(j).getPosition().trim())) {
                    listBeanList.add(bean.getJiankkElefailinsprecDivList().get(j));
                } else {
                    i = j - 1;
                    break;
                }
            }
            parentBean.setChildList(listBeanList);
            parentBeanList.add(parentBean);
            continue;

        }
        shebeiXunJianBean.setParentBeanList(parentBeanList);
    }
    @Override
    public void setListener() {
    }
    public class MyChildAdapter extends RecyclerView.Adapter<MyChildAdapter.ViewHolder> {
        ShebeiXunJianBean.ParentBean parentBean;
        Item item;

        public MyChildAdapter(ShebeiXunJianBean.ParentBean parent, Item item) {
            this.parentBean = parent;
            this.item = item;
        }

        @Override
        public MyChildAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_jdmx_detail_child, parent, false);
            return new MyChildAdapter.ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(MyChildAdapter.ViewHolder holder, int position) {
            holder.tv_text.setText(parentBean.getChildList().get(position).getEquipmentName());
            holder.tv_zhuangtai.setText(parentBean.getChildList().get(position).getFaultSituationText());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivityForResult(new Intent(JdxjDetailActivity.this, XjMingXiMuShowDetailActivity.class)
                                    .putExtra("data", shebeiXunJianBean)
                                    .putExtra("position", item.getParentPosition(position))
                            , Activity.RESULT_CANCELED);
                }
            });
        }

        @Override
        public int getItemCount() {
            return parentBean.getChildList().size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.tv_zhuangtai)
            TextView tv_zhuangtai;
            @BindView(R.id.tv_title_child)
            TextView tv_text;

            ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }
        }
    }
    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
        ShebeiXunJianBean shebeiXunJianBean;

        public void setShebeiXunJianBean(ShebeiXunJianBean shebeiXunJianBean) {
            this.shebeiXunJianBean = shebeiXunJianBean;
            notifyDataSetChanged();
        }

        @Override
        public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_jdcj_detail_layout, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(MyAdapter.ViewHolder holder, final int position) {
            holder.titleText.setText(shebeiXunJianBean.getParentBeanList().get(position).getProLarge());
//            holder.typeText.setText(shebeiXunJianBean.getJiankkElefailinsprecDivList().get(position).getFaultSituationText());
//            holder.xiangText.setText(shebeiXunJianBean.getJiankkElefailinsprecDivList().get(position).getEquipmentName());
//            holder.tv_text.setText(shebeiXunJianBean.getJiankkElefailinsprecDivList().get(position).getEquipmentName());
            holder.tv_num.setText(position+1+"");
            ShebeiXunJianBean.ParentBean parentBean=shebeiXunJianBean.getParentBeanList().get(position);
            MyChildAdapter myChildAdapter = new MyChildAdapter(parentBean, new Item() {
                @Override
                public int getParentPosition(int p) {
                    int pos=shebeiXunJianBean.getJiankkElefailinsprecDivList().indexOf(parentBean.getChildList().get(p));
                    return pos;
                }
            });
            holder.rv_child.setVisibility(View.VISIBLE);
            holder.rv_child.setAdapter(myChildAdapter);
            holder.rv_child.setLayoutManager(new LinearLayoutManager(holder.rv_child.getContext()));
            holder.re_spand.setVisibility(View.GONE);
            holder.iv_arrow.setImageResource(R.mipmap.arrow_d);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (holder.re_spand.getVisibility() == View.VISIBLE) {
                        holder.re_spand.setVisibility(View.GONE);
                        holder.iv_arrow.setImageResource(R.mipmap.arrow_d);
                    } else {
                        holder.re_spand.setVisibility(View.VISIBLE);
                        holder.iv_arrow.setImageResource(R.mipmap.arrow_u);
                    }
                }
            });
//            holder.re_spand.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    startActivity(new Intent(v.getContext(), XjMingXiMuShowDetailActivity.class)
//                            .putExtra("data", shebeiXunJianBean)
//                            .putExtra("position", position)
//                    );
//                }
//            });


//            holder.itemView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    startActivity(new Intent(v.getContext(), XjMingXiMuShowDetailActivity.class)
//                            .putExtra("data", shebeiXunJianBean)
//                            .putExtra("position", position)
//                    );
//                }
//            });
        }

        @Override
        public int getItemCount() {
            return shebeiXunJianBean == null ? 0 : shebeiXunJianBean.getParentBeanList().size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.titleText)
            TextView titleText;
            @BindView(R.id.xiangText)
            TextView xiangText;
            @BindView(R.id.typeText)
            TextView typeText;
            @BindView(R.id.itemRecyclerView)
            RecyclerView rv_child;
            @BindView(R.id.re_spand)
            RelativeLayout re_spand;
            @BindView(R.id.tv_text)
            TextView tv_text;
            @BindView(R.id.tv_left)
            TextView tv_left;
            @BindView(R.id.tv_remove)
            TextView tv_remove;  @BindView(R.id.tv_num)
            TextView tv_num;
            @BindView(R.id.iv_arrow)
            ImageView iv_arrow;
            ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }
        }
    }

    public class MyInAdapter extends RecyclerView.Adapter {
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_jdcj_detail_in_layout, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(JdxjJiluActivity.class);
                }
            });
        }

        @Override
        public int getItemCount() {
            return 3;
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.itemPostion)
            TextView itemPostion;
            @BindView(R.id.itemTitleName)
            TextView itemTitleName;
            @BindView(R.id.itemType)
            TextView itemType;

            ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }
        }
    }
//    public interface Item {
//        int getParentPosition(int position);
//    }
}
